<template>
  <div>
    <el-dialog
      v-model="props.visible"
      title="生产单列表"
      width="70%"
      @close="handleCancel"
    >
      <el-form
        :model="queryParams"
        class="flex-box"
        ref="queryRef"
        :inline="true"
      >
        <div>
          <el-form-item label="生产单编码" prop="produceNo">
            <el-input
              v-model.trim="queryParams.produceNo"
              placeholder="请输入生产单编码"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">
              搜索
            </el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </div>
        <!-- <el-button
          type="primary"
          plain
          @click="handleSubmit"
          :disabled="multiple"
        >
          确认
        </el-button> -->
      </el-form>
      <!-- @selection-change="handleSelectionChange" -->
      <el-row :gutter="20" style="overflow: auto">
        <el-col :span="24">
          <!-- @cell-click="handleClick" -->
          <el-table
            ref="orderRef"
            v-loading="loading"
            :data="orderList"
            stripe
            border
            row-key="produceId"
            style="cursor: pointer"
            @cell-click="handleClick"
          >
            <!-- <el-table-column
              type="selection"
              :reserve-selection="true"
              width="55"
            /> -->
            <el-table-column
              label="序号"
              width="55"
              type="index"
              align="center"
            />
            <el-table-column
              label="生产单编码"
              align="center"
              width="120"
              prop="produceNo"
            ></el-table-column>
            <el-table-column
              label="发货品种"
              align="center"
              min-width="120"
              prop="shipmentType"
            ></el-table-column>
            <el-table-column
              label="出版编码"
              width="100"
              prop="pubNo"
              align="center"
            ></el-table-column>
            <el-table-column
              label="书刊名称"
              min-width="160"
              prop="bookName"
              align="center"
            ></el-table-column>
            <el-table-column
              label="版材备注"
              min-width="160"
              prop="remark"
              align="center"
            ></el-table-column>
            <el-table-column
              label="创建人"
              width="100"
              prop="createBy"
              align="center"
            ></el-table-column>
            <el-table-column
              label="下单日期"
              width="100"
              align="center"
              :show-overflow-tooltip="true"
              prop="submitDate"
            >
              <template v-slot="{ row }">
                {{ row.submitDate ? row.submitDate.split("T")[0] : "" }}
              </template>
            </el-table-column>
          </el-table>
        </el-col>
        <!-- <el-col :span="12">
                    <el-table
                        ref="orderRef"
                        v-loading="loadingInfo"
                        :data="productList"
                        row-key="productId"
                        stripe
                        border
                        style="cursor: pointer"
                        @selection-change="handleSelectionChange"
                    >
                        <el-table-column type="selection" :reserve-selection="true" width="55" />
                        <el-table-column label="品种" prop="breed" align="center"></el-table-column>
                        <el-table-column
                            label="组成"
                            prop="composition"
                            align="center"
                        ></el-table-column>
                    </el-table>
                </el-col> -->
      </el-row>
      <div class="footer">
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { toNumber } from "@/utils/ruoyi";
import { getProduceById, getProduceOrderList } from "@/api/production/cover";
const emit = defineEmits(["changeUpdate", "cancel"]);
const { proxy } = getCurrentInstance();
const { mes_paper_specs, mes_paper_type, mes_paper_unit } = proxy.useDict(
  "mes_paper_unit",
  "mes_paper_type",
  "mes_paper_specs"
);

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  orderSelectList: {
    type: Array,
    default: () => {
      [];
    },
  },
});

const orderRef = ref(null);
const orderList = ref([]);
const productList = ref([]);
const multiple = ref(true);
const selectRow = ref([]);
const produceList = ref([]);
const loading = ref(false);
const loadingInfo = ref(false);
const orderListTable = ref(null);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
});
watch(
  () => props.visible,
  (val) => {
    if (val) {
      handleQuery();
      nextTick(() => {
        orderRef.value.clearSelection();
        // if (props.orderSelectList.length > 0) {
        //   props.orderSelectList.forEach((item) => {
        //     orderRef.value.toggleRowSelection(item, true);
        //   });
        // }
      });
    }
  }
);

const total = ref(0);
// 选中订单
const handleClick = (row) => {
  proxy.$modal
    .confirm("是否确认选择「" + row.produceNo + "」的生产单")
    .then(function () {
      emit("changeUpdate", row);
      // getProduceById({ pid: row.produceId }).then((res) => {
      //   emit("changeUpdate", res.data);
      //   loadingInfo.value = false;
      // });
    });
};

// 选中当前行
const handleCellClick = (row) => {
  row.breedList = [];
  proxy.$modal
    .confirm("是否确认选择「" + row.produceNo + "」")
    .then(function () {
      getProduceById({ pid: row.produceId }).then((res) => {
        res.data.produceProductList.forEach((ele) => {
          row.breedList.push(ele.breed);
        });

        emit("changeUpdate", row);
      });
    })
    .catch(() => {});
};

// 选中订单
const handleClicka = (row) => {
  proxy.$modal
    .confirm("是否确认选择「" + row.produceNo + "」的生产单")
    .then(function () {
      loadingInfo.value = true;
      getProduceById({ pid: row.produceId }).then((res) => {
        let sheet = 0;
        res.data.produceProductList.forEach((item) => {
          item.produceNo = row.produceNo; // 生产单编码
          item.breedType = item.breed;
          item.producePaperid = item.paperId;
          // item.sheet = 0;
          item.bookName = item.bookName;
          item.orderNo = item.orderNo;
          res.data.producePrintList.forEach((ele) => {
            sheet += Number(ele.sheet);
            // if (item.orderProductId == ele.orderProductId) {
            //   console.log(ele);

            // }
          });
        });
        productList.value = res.data.produceProductList;
        loadingInfo.value = false;
      });
    })
    .catch(() => {});
};

// 选中
const handleSelectionChange = (row) => {
  selectRow.value = row;
  multiple.value = !row.length;
};
// 选一个生产单
const handleSubmit = async () => {
  loading.value = true;
  const promises = selectRow.value.map((item) =>
    getProduceById({ pid: item.produceId })
  );
  const responses = await Promise.all(promises);
  loading.value = false;
  produceList.value = [];
  responses.forEach((response) => {
    response.data.sheet = 0;
    // response.data.produceProductList.forEach((item) => {
    response.data.producePrintList.forEach((ele) => {
      response.data.sheet += Number(ele.sheet);
    });
    // });
    // response.data.defaultCount = response.data.deliveryCount
    produceList.value.push(response.data);
  });
  emit("changeUpdate", produceList.value);
};
const handleCancel = () => {
  productList.value = [];
  proxy.resetForm("queryRef");
  emit("cancel");
};
const getList = async () => {
  loading.value = true;
  const res = await getProduceOrderList(queryParams.value);
  loading.value = false;
  orderList.value = res.rows;
  //   if (orderList.value.length > 0) {
  //     handleClick(orderList.value[0]);
  //   } else {
  //     productList.value = [];
  //   }
  total.value = res.total;
};
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};

// getList()
</script>

<style lang="scss" scoped></style>
